package com.zkingsoft.services.pc.impl;

import com.zkingsoft.services.pc.PcMenuManageService;
import com.zkingsoft.sqlUtil.ServiceUtil;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.model.pc.PcFoodMaterial;
import com.zkingsoft.model.pc.PcMenuFood;
import com.zkingsoft.model.pc.PcMenuManage;
import com.zkingsoft.dao.pc.PcMenuFoodDao;
import com.zkingsoft.dao.pc.PcMenuManageDao;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Map;

import com.zkingsoft.exception.GlobleException;
import com.zkingsoft.externalInterface.common.Util;
import com.zkingsoft.util.ModelUtils;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2017-03-14 14:37
 */
@Service("pcMenuManageService")
public class PcMenuManageServiceImpl implements PcMenuManageService {

	@Autowired
	private PcMenuManageDao pcMenuManageDao;
	@Autowired
	private PcMenuFoodDao pcMenuFoodDao;
	@Autowired
	private ServiceUtil serviceUtil;
	
	@Transactional
	@Override
	public int add(PcMenuManage pcMenuManage) {
		if (serviceUtil.addCheckRepeatTowColumn("pc_menu_manage", "men_name", pcMenuManage.getMenName()
				, "men_type", pcMenuManage.getMenType())) {
			throw new GlobleException("菜名已经存在");
		}
		int id = pcMenuManageDao.insert(pcMenuManage);
		addMenuFood(pcMenuManage);
		return id;
	}

	@Override
	public int batchAdd(List<PcMenuManage> pcMenuManageList) {
		int num = 0;

		int c = 10000;
		int size = pcMenuManageList.size() / c + 1;
		for (int i = 0; i < size; i++) {
			int begin = i * c;
			int end = (i + 1) * c;
			end = end >= pcMenuManageList.size() ? pcMenuManageList.size()
					: end;
			List<PcMenuManage> insertList = pcMenuManageList
					.subList(begin, end);
			num += pcMenuManageDao.batchInsert(insertList);
		}
		return num;

	}

	/**
	 * 存储原材料
	 */
	public void addMenuFood(PcMenuManage newPcMenuManage) {
		List<PcMenuFood> menuFood = newPcMenuManage.getMenuFood();
		PcMenuFood food = new PcMenuFood();
		food.setMenId(newPcMenuManage.getMenId());
		List<Long> arr = new ArrayList<>();
		for (PcMenuFood pmf : menuFood) {
			if (pmf != null && pmf.getFodId() != null) {
				if (!arr.contains(pmf.getFodId())) {
					arr.add(pmf.getFodId());
				} else {
					throw new GlobleException("存在重复的原材料");
				}
			}
		}
		pcMenuFoodDao.deleteByModel(food);
		for (PcMenuFood pmf : menuFood) {
			if (pmf != null && pmf.getFodId() != null
					&& pmf.getMenSingle() != null) {
				pmf.setMenId(newPcMenuManage.getMenId());
				pcMenuFoodDao.insert(pmf);
			}
		}
	}

	/**
	 * 存储单人用量和单位
	 */
	@Override
	public int modifyByMap(PcMenuManage oldPcMenuManage,
			PcMenuManage newPcMenuManage) {
		addMenuFood(newPcMenuManage);
		if (!ModelUtils.isModified(oldPcMenuManage, newPcMenuManage)) {
			return 1;
		}
		Map<String, Object> modifyMap = null;
		try {
			modifyMap = ModelUtils.comparePojo2Map(oldPcMenuManage,
					newPcMenuManage);
		} catch (Exception e) {
			throw new GlobleException("数据对比失败");
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("menId", oldPcMenuManage.getMenId());
			if(oldPcMenuManage.getMenType()!=newPcMenuManage.getMenType() || 
					oldPcMenuManage.getMenName()!=newPcMenuManage.getMenName()){
				if (serviceUtil.addCheckRepeatTowColumn("pc_menu_manage", "men_name", newPcMenuManage.getMenName()
						, "men_type", newPcMenuManage.getMenType())) {
					throw new GlobleException("菜名已经存在");
				}
			}
			
			pcMenuManageDao.updateByMap(modifyMap);
		}
		return 1;
	}

	@Override
	public int modifyByModel(PcMenuManage pcMenuManage) {
		
		return pcMenuManageDao.updateByModel(pcMenuManage);

	}

	@Override
	public int remove(List<Long> list) {
		PcMenuFood food = new PcMenuFood();
		for(Long id:list){
			food.setMenId(id);
			pcMenuFoodDao.deleteByModel(food);
		}
		return pcMenuManageDao.deleteByIds(list);

	}

	@Override
	public int removeById(Long menId) {

		return pcMenuManageDao.deleteById(menId);

	}

	@Override
	public int removeByModel(PcMenuManage pcMenuManage) {

		return pcMenuManageDao.deleteByModel(pcMenuManage);

	}

	@Override
	public List<PcMenuManage> findInPage(PcMenuManage pcMenuManage,
			PaginationVO pageVo) {

		return pcMenuManageDao.selectInPage(pcMenuManage, pageVo);

	}

	@Override
	public List<PcMenuManage> findByModel(PcMenuManage pcMenuManage) {

		return pcMenuManageDao.selectByModel(pcMenuManage);

	}

	@Override
	public int findTotal(PcMenuManage pcMenuManage) {

		return pcMenuManageDao.selectTotalRecord(pcMenuManage);

	}

	@Override
	public PcMenuManage findById(Long menId) {

		return pcMenuManageDao.selectById(menId);

	}

}